package com.jiexinyun.holder;

import org.redisson.api.RLock;

/**
 * Redis锁上下文持有者
 */
public class RedisLockContextHolder {

    private static final ThreadLocal<RLock> contextHolder = new ThreadLocal<>();

    public static RLock getContext() {
        return contextHolder.get();
    }

    public static void buildContext(RLock context) {
        contextHolder.set(context);
    }

    public static void clearContext() {
        RLock rLock = getContext();
        if (rLock != null && rLock.isLocked() && rLock.isHeldByCurrentThread()) {
            rLock.unlock();
        }
        if(rLock != null){
            contextHolder.remove();
        }
    }

}
